package com.felix.forum.service;

import com.felix.forum.NotFoundException;
import com.felix.forum.dao.ArticleRepository;
import com.felix.forum.po.Article;
import com.felix.forum.po.Type;
import com.felix.forum.util.MarkdownUtils;
import com.felix.forum.util.MyBeanUtils;
import com.felix.forum.vo.ArticleQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.*;

/**
 * @author Administrator
 * @date 2022/02/18 12:10
 **/
@Service
public class ArticleServiceImpl implements ArticleService{

    @Autowired
    private ArticleRepository articleRepository;

    @Override
    public Article getArticle(Long id) {
        return articleRepository.getOne(id);
    }

    @Transactional
    @Override
    public Article getAndConvert(Long id) {
        Article article = articleRepository.getOne(id);
        if (article == null) {
            throw new NotFoundException("该文献不存在！");
        }
        Article article1 = new Article();
        BeanUtils.copyProperties(article, article1);
        String content = article1.getContent();
        article1.setContent(MarkdownUtils.markdownToHtmlExtensions(content));

        articleRepository.updateViews(id);
        return article1;
    }

    @Override
    public Page<Article> listArticle(Pageable pageable, ArticleQuery article) {
        return articleRepository.findAll(new Specification<Article>() {
            @Override
            public Predicate toPredicate(Root<Article> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();
                if (!"".equals(article.getTitle()) && article.getTitle() != null) {
                    predicates.add(cb.like(root.<String>get("title"), "%"+article.getTitle()+"%"));
                }
                if (article.getTypeId() != null) {
                    predicates.add(cb.equal(root.<Type>get("type").get("id"), article.getTypeId()));
                }
                if (article.isRecommend()) {
                    predicates.add(cb.equal(root.<Boolean>get("recommend"), article.isRecommend()));
                }
                cq.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        }, pageable);
    }

    @Override
    public Page<Article> listArticle(Pageable pageable) {
        return articleRepository.findAll(pageable);
    }

    @Override
    public Page<Article> listArticle(Long tagId, Pageable pageable) {
        return articleRepository.findAll(new Specification<Article>() {
            @Override
            public Predicate toPredicate(Root<Article> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                Join join = root.join("tags");
                return cb.equal(join.get("id"), tagId);
            }
        }, pageable);
    }

    @Override
    public Page<Article> listArticle(String query, Pageable pageable) {
        return articleRepository.findByQuery(query, pageable);
    }

    @Override
    public List<Article> listRecommendArticleTop(Integer size) {
        Sort sort = Sort.by(Sort.Direction.DESC, "updateTime");
//        Pageable pageable = new PageRequest(0,size,sort);
        Pageable pageable = PageRequest.of(0, size, sort);
        return articleRepository.findTop(pageable);
    }

    @Override
    public Map<String, List<Article>> archiveArticle() {
        List<String> years = articleRepository.findGroupYear();
        Map<String,List<Article>> map = new HashMap<>();
        for (String year : years) {
            map.put(year,articleRepository.findByYear(year));
        }
        return null;
    }

    @Override
    public Long countArticle() {
        return articleRepository.count();
    }

    @Transactional
    @Override
    public Article saveArticle(Article article) {
        if (article.getId() == null) {
            article.setCreateTime(new Date());
            article.setUpdateTime(new Date());
            article.setViews(0);
        } else {
            article.setUpdateTime(new Date());
        }
        return articleRepository.save(article);
    }

    @Transactional
    @Override
    public Article updateArticle(Long id, Article article) {
        Article article1 = articleRepository.getOne(id);
        if (article1 == null) {
            throw new NotFoundException("该文章不存在！");
        }
        BeanUtils.copyProperties(article, article1, MyBeanUtils.getNullPropertyNames(article));
        article1.setUpdateTime(new Date());
        return articleRepository.save(article1);
    }

    @Transactional
    @Override
    public void deleteArticle(Long id) {
        articleRepository.deleteById(id);
    }
}
